﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OracleClient;

using Maticsoft.DBUtility;
using DQYT.HTDZ.Model;
using System.Data;



namespace DQYT.HTDZ.Web.Views.LSZD
{
    public partial class ReviewForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("../../Default.aspx");
            }
           
            string page = Request.QueryString["currPage"];
           
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (id == "undefined")
                {
                    HttpContext.Current.Response.Write("<Script language='JavaScript'>alert('请选择一条数据');window.close();</script>");
                    return;
                }
                string strSql = "select wellid,jcrq from bddatesourse where id= " + id;
                DataTable dt = DbHelperOra.Query(strSql).Tables[0];
                if (dt.Rows.Count < 1)
                    return;
                this.lbl_jh.Text = dt.Rows[0][0].ToString().Trim();
                this.lbl_cjrq.Text = dt.Rows[0][1].ToString().Trim();
                Session["ZD_WELL_ID"] = dt.Rows[0][0].ToString().Trim();
                Session["ZD_JCRQ"] = dt.Rows[0][1].ToString().Trim();

                if (!string.IsNullOrEmpty(page))
                {
                    //绑定数据
                    InitReviewList(int.Parse(page));

                }
                else
                {
                    InitReviewList(1);
                }
            }
           
           
        }

        #region 绑定数据列表
        /// <summary>
        /// 绑定数据列表
        /// </summary>
        /// <param name="currentPage"></param>
        private void InitReviewList(int currentPage)
        {
            string strWellId = Session["ZD_WELL_ID"] == null ? "" : Session["ZD_WELL_ID"].ToString().Trim();
            string strWhere = "trim(wellid)='" + strWellId + "'" + "order by JCRQ desc ";          
            DataTable dt = GetList(strWhere).Tables[0];           
            int count = dt.Rows.Count;
            int pageCount = 5;

            int pageAll = count / pageCount + (count % pageCount > 0 ? 1 : 0);

            lblALLcount.Text = Convert.ToString(count);//一共多少条
            lblendcount.Text = Convert.ToString(currentPage * pageCount);
            lblfirstcoutn.Text = Convert.ToString(pageCount * (currentPage - 1) + 1);//第几页的第一条的数字
            lblpagecount.Text = Convert.ToString(currentPage);
            ViewState["wy"] = pageAll;

            dgvTest.DataSource = GetListByPage(pageCount, currentPage, Session["ZD_WELL_ID"].ToString()).Tables[0];
            dgvTest.DataBind();       
        
        }
        #endregion

        #region 分页功能
        protected void libSY_Click(object sender, EventArgs e)
        {
            InitReviewList(1);
        }

        protected void libsyy_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblpagecount.Text) > 1)
            {
                InitReviewList(Convert.ToInt32(lblpagecount.Text) - 1);
            }
        }

        protected void libxyy_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblpagecount.Text) < Convert.ToInt32(ViewState["wy"]))
            {
                InitReviewList(Convert.ToInt32(lblpagecount.Text) + 1);
            }
        }

        protected void libwy_Click(object sender, EventArgs e)
        {
            InitReviewList(Convert.ToInt32(ViewState["wy"]));
        }
        #endregion

        #region 信息删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Lbt_Del_Click(object sender, EventArgs e)
        {
            string delId = ((LinkButton)sender).CommandArgument;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bdtepzdjy ");
            strSql.Append(" where id=:ZDID ");
            OracleParameter[] parameters = {
					new OracleParameter(":ZDID", OracleType.VarChar,30)			};
            parameters[0].Value = delId;

            DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
          
            //绑定数据
            InitReviewList(Convert.ToInt32(lblpagecount.Text));
        }
        #endregion

        #region 信息修改跳转
        /// <summary>
        /// 修改跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string zd_id = ((LinkButton)sender).CommandArgument; 
            DataTable dt =getAdviseByID(zd_id);           
            string zdr = dt.Rows[0]["ZDR"].ToString();
            //诊断为本人录入的才可以修改
            if (zdr.Equals(Session["UserName"]))
            {
                string para = "?zdid=" + zd_id ;
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "updateAdvise", "<script>updateAdvise('" + para + "');</script>", false);
                //this.Page.RegisterStartupScript("", "<script>updateAdvise('" + para + "');</script>");               
            }          
            
        }
        #endregion       

        #region 报表编制跳转
        /// <summary>
        /// 报表编制跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            string zd_id = ((LinkButton)sender).CommandArgument;
            string strWellId = this.lbl_jh.Text.Trim();           
            DataTable dt =getAdviseByID(zd_id); 

            string strJcrq = dt.Rows[0]["JCRQ"].ToString().Trim();
            string strZdsj = dt.Rows[0]["ZDSJ"].ToString().Trim();
            if (String.IsNullOrEmpty(strZdsj))
            {
                strZdsj = DateTime.Now.ToString();
            }
            string para = "?wellid=" + strWellId + "&jcrq=" + strJcrq + "&zdsj=" + strZdsj;
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "jumpToParent", "<script>jumpToParent('" + para + "');</script>", false);
            // this.Page.RegisterStartupScript("", "<script>jumpToParent('" + para + "');</script>");

        }
        #endregion

        #region 按钮显示问题
        /// <summary>
        /// 按钮显示问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgvTest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells != null)
            {

                string zd_id = dgvTest.DataKeys[e.Row.RowIndex].Value.ToString();

                if (!string.IsNullOrEmpty(zd_id))
                {                   
                    DataTable dt = getAdviseByID(zd_id);
                    string zdr = dt.Rows[0]["ZDR"].ToString();

                    if (zdr.Equals(Session["UserName"]))
                    {//登陆为本人时，修改、删除按钮显示
                        e.Row.FindControl("LinkButton1").Visible = true;
                        e.Row.FindControl("Lbt_Del").Visible = true; 
                    }
                    else
                    {
                        e.Row.FindControl("LinkButton1").Visible = false;
                        e.Row.FindControl("Lbt_Del").Visible = false;   
                    }
                }

            }
        }
        #endregion    

        #region 根据诊断主键ID获取信息
        /// <summary>
        /// 根据主键ID获取详细信息
        /// </summary>
        /// <param name="zd_id"></param>
        /// <returns></returns>
        protected DataTable getAdviseByID(string zd_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,WELLID,ZDSJ,JCRQ,ZDJY,ZDR from bdtepzdjy");
            strSql.Append(" where id=:id ");
            OracleParameter[] parameters = {
                    new OracleParameter(":id", OracleType.VarChar,30) };
            parameters[0].Value = zd_id;

            DataTable dt = DbHelperOra.Query(strSql.ToString(), parameters).Tables[0];
            return dt;
        }
        #endregion

        #region 获取列表 不分页
        /// <summary>
        /// 获得数据列表
        /// </summary>
        protected DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,WELLID,ZDSJ,JCRQ,ZDJY,ZDR ");
            strSql.Append(" FROM bdtepzdjy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }
        #endregion 

        #region 分页获取诊断信息列表
        /// <summary>
        /// 分页获取信息
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="currPage"></param>
        /// <param name="wellid"></param>
        /// <returns></returns>
        protected DataSet GetListByPage(int pageSize, int currPage, string wellid)
        {
            StringBuilder sbSql = new StringBuilder();

            sbSql.Append(" select * from bdtepzdjy where rowid in (select rid from ( ");          

            sbSql.Append("  select rownum rn,rid from (select rowid rid,ID,wellid,zdjy  from bdtepzdjy  ");
           
            sbSql.Append("  where 1=1 ");            

            if (!string.IsNullOrEmpty(wellid))
            {
                sbSql.Append(string.Format("  and wellid = '{0}' ", wellid));
            }
            sbSql.Append("  order by JCRQ,id desc) ");

            sbSql.Append(string.Format("  where rownum <=  '{0}' )", currPage * pageSize));

            sbSql.Append(string.Format("  where rn >=  '{0}' )", currPage * pageSize - (pageSize - 1)));  
        
            sbSql.Append("  order by JCRQ,id desc ");

            return DbHelperOra.Query(sbSql.ToString());          
           
        }     

        #endregion
              
    }
}